package com.fortune.service.ananlysis;

import com.faneui.common.exception.BusinessException;
import com.fortune.service.ananlysis.dto.BillMailInfoDTO;
import com.fortune.service.ananlysis.dto.TradeRecordDTO;
import com.google.common.collect.Lists;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

// 中国工商银行
@Service("icbcCreditCardBillMailAnanlysis")
public class IcbcCreditCardBillMailAnanlysis  extends AbstractAnanlysis{

    @Override
    void ananlysis(BillMailInfoDTO billMailInfoDTO, Document document) throws BusinessException {
        //客户姓名
        Elements es = document.select("p:contains(尊敬的)").select("[class=first]");
        Element el = es.first();
        String st = el.text();
        String name = st.substring(st.indexOf("尊敬的") + 3,st.indexOf(",您好!")).replace("先生","").replace("女士","");
//        System.out.println(name);
        billMailInfoDTO.setCustName(name);
        System.out.println(name);
            //最后还款日
        Element e = document.select("td:contains(贷记卡到期还款日)").select("[width=50%]").first();
                Element e1 = e.parent().child(2);
                String time1 = e1.text();
                System.out.println("----------");
                System.out.println(time1);
                String year = time1.substring(0,4);
                String month = time1.substring(time1.indexOf("年") + 1,time1.indexOf("月"));
                String day = time1.substring(time1.indexOf("月") + 1 , time1.indexOf("日"));
                if(month.length() == 1){
                     month = "0" + month;
                }
                if(day.length() == 1){
                    day = "0" + day;
                }
                billMailInfoDTO.setDeadlineDate(year + month + day);



//            if(e.text().contains("需 还 款 明 细")){
        Element e2 = document.select("table:contains(需 还 款 明 细)").select("[width=100%]").first();
                Elements e3 = e2.parent().child(5).select("td");
                String card = e3.get(5).text();
                //卡名称
                billMailInfoDTO.setCardName(card.substring(card.indexOf("(") + 1,card.indexOf(")")));
                //卡号
                billMailInfoDTO.setCardCode("*" + card.substring(0,4));
                //总还款额
                String totalDebtMoney = e3.get(7).text().replace(",","").replace("/RMB","");
                BigDecimal tdm = new BigDecimal(totalDebtMoney);
                billMailInfoDTO.setTotalAmount(tdm);
                //最低还款
                String minDebtMoney = e3.get(8).text().replace(",","").replace("/RMB","");
                BigDecimal mdm = new BigDecimal(minDebtMoney);
                billMailInfoDTO.setMinRepayAmount(mdm);
                //信用额度
                String creditLimit = e3.get(9).text().replace(",","").replace("/RMB","");
                BigDecimal cl = new BigDecimal(creditLimit);
                billMailInfoDTO.setMinRepayAmount(cl);
//            }

            //积分额度

        Element e4 = document.select("b:contains(个人综合积分)").first();
                Element e5 = e4.parent().parent().parent().select("td:contains(余额)").first();
                String point = e5.text().substring(3).replace(",","");
                BigDecimal p = new BigDecimal(point);
                billMailInfoDTO.setPoint(p);

                Element e6 = document.select("b:contains(---主卡明细---)").first();
                Elements e7 = e6.parent().parent().parent().select("tr");
        List<TradeRecordDTO> recordDTOList = Lists.newArrayList() ;
         for(int i = 2 ; i < e7.size() ; i++){
             TradeRecordDTO tradeRecordDTO = new TradeRecordDTO();
             tradeRecordDTO.setTradeDate(e7.get(i).child(1).text().replace("-",""));
             tradeRecordDTO.setBookDate(e7.get(i).child(2).text().replace("-",""));
             tradeRecordDTO.setMerchantName(e7.get(i).child(4).text());
             String money =e7.get(i).child(5).text().replace("/RMB","").replace(",","");
             BigDecimal money1 = new BigDecimal(money);
             tradeRecordDTO.setMoney(money1);

             System.out.println(tradeRecordDTO);
             recordDTOList.add(tradeRecordDTO);
         }
        billMailInfoDTO.setTradeRecordDTOS(recordDTOList);
    }

}
